package dtd.PHS.VND_ExchangeRate;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import dtd.PHS.VND_ExchangeRate.SimpleGestureFilter.SimpleGestureListener;
import dtd.PHS.VND_ExchangeRate.VCB_ERate.MyCurrency;
import dtd.PHS.VND_ExchangeRate.VCB_ERate.VND_ExchangeRate;



public class ListRates extends Activity implements SimpleGestureListener{

	private static final int CONTEXTMENU_SENDSMS = 1;
	private ListView lvMain;
	private VCB_ERate vcb;
	private TextView tvDate;
	private MyMainMenu mainMenu;
	private SimpleGestureFilter detector;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyUtility.callOnEveryActivity(this);
		setContentView(R.layout.list_rates);
		try {	    
			lvMain = (ListView)findViewById(R.id.lvMainList);
			vcb = new VCB_ERate(this);
			mainMenu = new MyMainMenu(this);
			
			this.updateDateText();
			RowAdapter adapter = new RowAdapter(this, R.layout.row_item, MyUtility.getCurrenciesExceptVND());
			lvMain.setAdapter(adapter);   	
			lvMain.setOnCreateContextMenuListener(new MyOnCreateContextMenuListener());
			this.detector = new SimpleGestureFilter(this, this);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public String getDate() {
		return (String)tvDate.getText();
	}
	private void updateDateText() {
		this.tvDate = (TextView)findViewById(R.id.tvListRateDate);
		tvDate.setText(tvDate.getText().toString() +" "+ vcb.getInputDate());		
	}

	public void onClickConverterHandler(View view) {
		Intent intent = new Intent(this,CalculateRate.class);
		this.startActivity(intent);	
	}
	public void onClickSMSHandler(View view) {
		Intent intent = new Intent(this,SendSMS.class);
		this.startActivity(intent);	
	}

	public void onClickHandler(View view) {
		//		String cstr = getCurrencyInfo( view );
		//		VND_ExchangeRate rates = vcb.getVNDRateOf(cstr);
		//		String content = MyUtility.createSMSContent(this,cstr,rates,vcb.getInputDate());
		//		MyUtility.broadcastSMSIntent( this,content );
	}

	//	private String getCurrencyInfo(View view) {
	//		return (String)view.getTag();		
	//	}

	public boolean onCreateOptionsMenu(Menu menu) {
		mainMenu.createOptionsMenu(menu);
	    return true;
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		return mainMenu.onItemSelected(item);	
	}
		

	
	@Override
	public boolean onContextItemSelected(MenuItem aItem) {
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();
		switch (aItem.getItemId()) {
		case CONTEXTMENU_SENDSMS:
			MyCurrency c = (MyCurrency)lvMain.getAdapter().getItem(menuInfo.position);
			String content = MyUtility.createSMSContent(this, c.toString(), vcb.getVNDRateOf(c), vcb.getInputDate());
			MyUtility.broadcastSMSIntent(this, content);
			return true;
		}
		return false;
	}
	private class RowAdapter extends ArrayAdapter<MyCurrency> {

		public RowAdapter(Context context, int textViewResourceId,
				MyCurrency[] currencies) {
			super(context, textViewResourceId, currencies);			
		}

		@Override
		public View getView(
				int position,
				View convertView,
				ViewGroup parent ) {
			RowItem v = (RowItem) convertView;
			if ( v == null ) {				
				v = new RowItem(getContext());
			}
			MyCurrency c = getItem(position);
			if ( c != null ) {
				v.setTags(c);
				VND_ExchangeRate rate = vcb.getVNDRateOf(c);				
				v.setBuyingRate(MyUtility.formatRate(rate.buying));
				v.setTransferRate(MyUtility.formatRate(rate.transfer));
				v.setSellingRate(MyUtility.formatRate(rate.selling));
				v.setFlag(c);
			}
			return v;
		}

	}

	private class MyOnCreateContextMenuListener implements
	OnCreateContextMenuListener {
	
		public void onCreateContextMenu(ContextMenu menu, View v,
				ContextMenuInfo menuInfo) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			String currency = (String)(info.targetView.getTag());
			menu.setHeaderTitle(v.getContext().getString(R.string.ListRate_ContextMenuTitle) + " " + currency);
			menu.add(
					0, CONTEXTMENU_SENDSMS, 
					0 , v.getContext().getString(R.string.ListRate_SendSMS));
		}

	}
	
	@Override 
	public boolean dispatchTouchEvent(MotionEvent me){ 
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me); 
	}
	
	@Override
	public void onDoubleTap() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onSwipe(int direction) {
		switch (direction) {
		case SimpleGestureFilter.SWIPE_LEFT:
			this.startActivity(new Intent(this,CalculateRate.class));
			break;
		case SimpleGestureFilter.SWIPE_RIGHT:
			this.startActivity(new Intent(this,SendSMS.class));
			break;
		default:
			break;
		}
		
	}
//	public void onCreateContextMenu(ContextMenu menu, View v,
//		    ContextMenuInfo menuInfo) {
//		  if (v.getId()==R.id.list) {
//		    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
//		    menu.setHeaderTitle(Countries[info.position]);
//		    String[] menuItems = getResources().getStringArray(R.array.menu);
//		    for (int i = 0; i<menuItems.length; i++) {
//		      menu.add(Menu.NONE, i, i, menuItems[i]);
//		    }
//		  }
//		}
}

